package com.ds.smartstudio.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ds.smartstudio.controller.DTO.UserDTO;
import com.ds.smartstudio.entity.User;
import com.ds.smartstudio.mapper.UserMapper;
import com.ds.smartstudio.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ds.smartstudio.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangsf
 * @since 2022-07-02 10:42:24
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public Integer selectTotalByName(String name) {
        return userMapper.selectTotalByName(name);
    }

    @Override
    public List userPage(int page, int pageSize, String name) {
        Page<User> userPage = new Page<>(page, pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("ds_user_id")
                .like("ds_user_name", name);
        this.page(userPage, wrapper);
        List<User> users = userPage.getRecords();
        List<Object> data = new ArrayList<>();
        data.add(users);
        data.add(userMapper.selectTotalByName(name));

        return data;
    }

    @Override
    public UserDTO login(UserDTO userDTO) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("ds_user_name", userDTO.getUsername())
                .eq("ds_user_password", userDTO.getPassword());
        User user = null;
        try {
            user = getOne(wrapper);
            if (user != null) {
                userDTO.setUsername(user.getDsUserName());
                String token = TokenUtils.genToken(user.getDsUserId().toString(), user.getDsUserPassword());
                userDTO.setToken(token );
                userDTO.setPassword(null);
            } else {
                userDTO = null;
            }
        } catch (Exception e) {
            log.error(e.toString());
            userDTO = null;
        }
        return userDTO;
    }

    @Override
    public User selectOneByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("ds_user_name", name);
        User one = null;
        try {
            one = this.getOne(wrapper);
        } catch (Exception e) {
            log.error(e.toString());
        }
        return one;
    }

    @Override
    public boolean removeById(User entity) {
        if (entity.getDsUserRole() == 1) {
            if(userMapper.countAdmin()==1)
                return false;
            else
                return super.removeById(entity);
        } else {
            return super.removeById(entity);
        }
    }
}
